var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require('cors');
var { expressjwt: jwt } = require('express-jwt')
var bodyparser = require("body-parser")

var app = express();

// 配置跨域
app.use(cors())

// post 参数最大携带量
app.use(bodyparser.json({ limit: '3mb' }))
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// app.use(
//   jwt({
//     credentialsRequired: true, //需要校验
//     secret: "yv", // 加密秘钥
//     algorithms: ["HS256"], // 加密方式
//   }).unless({
//     path: [
//       { url: /^\/upload/, methods: ["GET"] },
//       { url: "/gjl/teacherLogin", methods: ["POST"] },
//       { url: /^\/static/, methods: ["GET"] },
//     ],
//   })
// );
app.use(express.static(path.join(__dirname, 'public')));
//虚拟路由创建文件夹
app.use("/upload", express.static("upload"))
app.use(cors())

var gjl_Router = require('./routes/gjl');
var twk_Router = require('./routes/twk');
var zxj_Router = require('./routes/zxj');
var zt_Router = require('./routes/zt');
var zhy_Router = require('./routes/zhy')
var ljr_Router = require('./routes/ljr')


app.use('/api/gjl', gjl_Router);
app.use('/api/twk', twk_Router);
app.use('/api/zxj', zxj_Router);
app.use('/api/zt', zt_Router);
app.use('/api/zhy', zhy_Router);
app.use('/api/ljr', ljr_Router);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  console.log(err)
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;